]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
util: Use (thread-safe) uselocale()
authorFlorian Müllner <fmuellner@gnome.org>
Tue, 11 Sep 2018 01:17:20 +0000 (03:17 +0200)
committerFlorian Müllner <fmuellner@gnome.org>
Wed, 30 Jan 2019 19:35:04 +0000 (20:35 +0100)
Our helper method to translate a string according to the LC_TIME
setting uses the no-thread-safe setlocale(). While that's not an
issue in practice given that all JS code shares a single thread,
I was pointed to a better alternative when adopting the same
handling in gnome-desktop's wallclock[0].

[0] https://gitlab.gnome.org/GNOME/gnome-desktop/merge_requests/17

https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/292

src/shell-util.c

index dfc13cd2120bd2c76e3aa44097774db20f4c2b97..ed659307814a2650b829c787e130d93c0147e346 100644 (file)
@@ -235,14 +235,21 @@ shell_util_translate_time_string (const char *str)
   const char *locale = g_getenv ("LC_TIME");
   const char *res;
   char *sep;
+  locale_t old_loc;
+  locale_t loc = (locale_t) 0;
 
   if (locale)
-    setlocale (LC_MESSAGES, locale);
+    loc = newlocale (LC_MESSAGES_MASK, locale, (locale_t) 0);
+
+  old_loc = uselocale (loc);
 
   sep = strchr (str, '\004');
   res = g_dpgettext (NULL, str, sep ? sep - str + 1 : 0);
 
-  setlocale (LC_MESSAGES, "");
+  uselocale (old_loc);
+
+  if (loc != (locale_t) 0)
+    freelocale (loc);
 
   return res;
 }